#pragma once
#include "reactor/noncopyable.hpp"
#include <functional>

namespace reactor {
class EventLoop;

/**
 * @brief 文件描述符的封装，包含事件和回调
 */
class Channel : noncopyable {
public:
    using EventCallback = std::function<void()>;

    Channel(EventLoop* loop, int fd);
    ~Channel();

    // 根据 _revents 调用不同的回调函数
    void handleEvent();

    void setReadCallback(EventCallback cb) { _readCallback = std::move(cb); }
    void setWriteCallback(EventCallback cb) { _writeCallback = std::move(cb); }
    void setCloseCallback(EventCallback cb) { _closeCallback = std::move(cb); }
    void setErrorCallback(EventCallback cb) { _errorCallback = std::move(cb); }

    int fd() const { return _fd; }
    int events() const { return _events; }
    void setRevents(int revt) { _revents = revt; }
    bool isNoneEvent() const { return _events == kNoneEvent; }

    void enableReading();
    void disableReading();
    void enableWriting();
    void disableWriting();
    void disableAll();
    bool isWriting() const;

    EventLoop* ownerLoop() { return _loop; }
    void remove();

private:
    void update();

    static const int kNoneEvent;
    static const int kReadEvent;
    static const int kWriteEvent;

    EventLoop* _loop;
    const int _fd;
    int _events;
    int _revents;

    EventCallback _readCallback;
    EventCallback _writeCallback;
    EventCallback _closeCallback;
    EventCallback _errorCallback;
};
} // namespace reactor
